﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.DAO;
using ClassLibrary.Entity;
using ClassLibrary.Data;
using System.Collections.Generic;

public partial class ucControl_ucShipTo : System.Web.UI.UserControl
{
    TblShippToDAO shiptoDAO = new TblShippToDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            panel.Attributes.Add("style", "display:none");
            if (Session[Global.SESSION_CART].ToString() == null || Session[Global.SESSION_LOGGED_USER].ToString() == null)
            {
                Response.Redirect("~/Default.aspx");
            }
            else
            {
                txt_email.Text = ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).Email;
                txt_fistname.Text = ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).FirstName;
                txt_lastname.Text = ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).LastName;
                txtmobilephone.Text = ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).Phone;
                txt_address.Text = ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).Address;


            }
        }
    }

    // Load ShipTo, nếu đã có
    private void LoadShippingTo()
    {
        try
        {
            TblUserAccount account = new TblUserAccount();
            if (Session[Global.SESSION_LOGGED_USER] != null)
                account = (TblUserAccount)Session[Global.SESSION_LOGGED_USER];

            TblShippTo shipto = new TblShippTo();

            if (Session[Global.SESSION_SHIPTO] == null)
                shipto = shiptoDAO.GetAll(account.AccountID);
            else
                shipto = (TblShippTo)Session[Global.SESSION_SHIPTO];
            if (shipto != null)
            {
                txtho.Text = shipto.FName;
                txtten.Text = shipto.LName;
                txtmobile.Text = shipto.MobilePhone;
                txtemail.Text = shipto.EmailAdress;
                txtdiachi.Text = shipto.CusAddress;
            }
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        bool flag = false;
        int type = 0;
        int method = 0;
        try
        {
            if (cb1.Checked)
            {

                if (!Page.IsValid) return;
                TblShippTo shipto;
                TblUserAccount account = new TblUserAccount();

                if (Session[Global.SESSION_SHIPTO] == null)
                    shipto = new TblShippTo();
                else
                    shipto = (TblShippTo)Session[Global.SESSION_SHIPTO];

                if (Session[Global.SESSION_LOGGED_USER] != null)
                    account = (TblUserAccount)Session[Global.SESSION_LOGGED_USER];
                shipto.CusID = account.AccountID;
                shipto.CusAddress = account.Address;
                shipto.EmailAdress = ((TblUserAccount)Session[Global.SESSION_LOGGED_USER]).Email;
                shipto.FName = account.FirstName;
                shipto.LName = account.LastName;
                shipto.MobilePhone = account.Phone;

                Session[Global.SESSION_SHIPTO] = shipto;
                type = 1;

                Session["type"] = type;
            }

            else
            {
                TblShippTo shipto1 = new TblShippTo();
                TblUserAccount account1 = new TblUserAccount();

                if (Session[Global.SESSION_LOGGED_USER] != null)
                    account1 = (TblUserAccount)Session[Global.SESSION_LOGGED_USER];
                shipto1.CusID = account1.AccountID;
                shipto1.CusAddress = txtdiachi.Text;
                shipto1.EmailAdress = txtemail.Text;
                shipto1.FName = txtho.Text;
                shipto1.LName = txtten.Text;
                shipto1.MobilePhone = txtmobile.Text;

                Session[Global.SESSION_SHIPTO1] = shipto1;

                type = 2;

                Session["type"] = type;

            }
            List<TblBookingDetail> detail_lst = (List<TblBookingDetail>)Session[Global.SESSION_CART];
            //foreach (TblBookingDetail detail in detail_lst)
            //{
            //}

            Session[Global.SESSION_CART] = detail_lst;

            //Shipping Method

            if (rdThanhtoansau.Checked)
            {
                //Session[Global.SESSION_SHIPPINGMETHOD] = "1";
                method = 1;
                Session["method"] = method;
            }
            else
            {
                //Session[Global.SESSION_SHIPPINGMETHOD] = "2";
                method = 2;
                //Session[Global.SESSION_SHIPPINGMETHOD] = method;
                Session["method"] = method;
            }

            flag = true;
        }
        catch (Exception ex)
        {
            Session[Global.SESSION_ERROR] = ex;
            Response.Redirect(Global.ERROR_PAGE, true);
        }

        if (flag)
        {
            Response.Redirect(string.Format("~/XacNhan.aspx"), true);
        }
    }
}
